function check_add_hdct(id_row,add,tien_sua,idsua,tongtiensua,tienvatsua){
	var clsp = document.getElementById('lsp_id_'+id_row);
	var sp = document.getElementById('Sph_Id_'+id_row);
	var soluong = document.getElementById('soluong_'+id_row);
	var donhang = COREJS._getJsLang('donhang');
	var gia_ban = document.getElementById('giaban_'+id_row+'_');
	var view = COREJS._getJsLang('view');
	var vat = $('#vat_'+id_row);
	if(clsp.value==''){
		alert(COREJS._getJsLang('_BAN_CHUA_CHON_CLSP'));
		clsp.focus();
		return false;
	}
	if(sp.value==""){
		alert(COREJS._getJsLang('_BAN_CHUA_CHON_SP'));
		sp.focus();
		return false;
	}
	if(gia_ban.value <=0 ){
		alert(COREJS._getJsLang('_GIA_BAN_PHAI_DUONG'));
		gia_ban.focus();
		return false;
	}
	if(soluong.value==""){
		alert(COREJS._getJsLang('_BAN_CHUA_NHAP_SO_LUONG'));
		soluong.focus();
		return false;
	}
	if(soluong.value<=0){
		alert(COREJS._getJsLang('_SO_LUONG_PHAI_LON_HON_KHONG'));
		soluong.focus();
		return false;
	}
	if(vat.val()*1<0){
		alert(COREJS._getJsLang('_VAT_PHAI_LON_HON_HOAC_BANG_KHONG'));
		vat.focus();
		return false;
	}
	else{
		var gia_tri_cu=document.getElementById('giatri_').value;
		var thanhthien=document.getElementById('thanhthien_'+id_row).value;
		//gia tri hop dong
		var gia_tri_moi=gia_tri_cu*1+thanhthien*1-tien_sua*1;
		document.getElementById('giatri_').value=gia_tri_moi;
		if(donhang!=0){
			tien_chua_tt=0;
			document.getElementById('giatri').innerHTML=formatNumber_DB(gia_tri_moi);
			//tien vat 
			/*tienvat_cu=document.getElementById('sum_tien_vat').value;
			tien_vat_moi=document.getElementById('tien_vat_'+id_row).value;
			tong_tien_vat=tienvat_cu*1+tien_vat_moi*1-tienvatsua*1;
			document.getElementById('tien_vat_dd').innerHTML=formatNumber_DB(tong_tien_vat);
			document.getElementById('sum_tien_vat').value=tong_tien_vat;
			//tong tien
			tongtien_cu=document.getElementById('sum_tong_tien').value;
			tongtien_moi=document.getElementById('tien_tong_'+id_row).value;
			tong_tien=tongtien_cu*1+tongtien_moi*1-tongtiensua*1;
			document.getElementById('tong_tien_dd').innerHTML=formatNumber_DB(tong_tien);
			document.getElementById('sum_tong_tien').value=tong_tien;*/
		}
		else{
			
			var tien_da_tt=document.getElementById('tiendatt_').value;
			tien_chua_tt=gia_tri_moi*1-tien_da_tt*1;
			document.getElementById('tienphaitt_').value=tien_chua_tt;
			if(view == 1){
				document.getElementById('giatri').innerHTML=formatNumber_DB(gia_tri_moi);
				document.getElementById('tienphaitt').innerHTML=formatNumber_DB(tien_chua_tt);
			}
			else{
				document.getElementById('tienphaitt').value=formatNumber_DB(tien_chua_tt);
				document.getElementById('giatri').value=formatNumber_DB(gia_tri_moi);
			}
		}
		var getFormValues = '';
			getFormValues +='&count_del_hdct='+$('#count_del_hdct').val();
			getFormValues +='&id_table='+$('#id_table').val();
			getFormValues +='&table='+$('#table').val();
			getFormValues +='&table='+$('#table').val();
			getFormValues +='&Cls_Id='+$('#lsp_id_'+id_row).val();
			getFormValues +='&Sph_Id='+$('#Sph_Id_'+id_row).val();
			getFormValues +='&giaban='+$('#giaban_'+id_row).val();
			getFormValues +='&giaban_='+$('#giaban_'+id_row+'_').val();
			getFormValues +='&soluong='+$('#soluong_'+id_row).val();
			getFormValues +='&Dvt_Id='+$('#dvt_id_'+id_row).val();
			getFormValues +='&Gia_Co_Dinh='+$('#gia_codinh_'+id_row).val();
			getFormValues +='&tong_tien_hdct='+$('#tien_tong_'+id_row).val();
			getFormValues +='&tien_vat='+$('#tien_vat_'+id_row).val();
			getFormValues +='&vat='+$('#vat_'+id_row).val();
			getFormValues +='&thanhtien='+$('#thanhthien_'+id_row).val();
			getFormValues +='&perm_edit='+$('input[name$="perm_edit"]').val();
			getFormValues +='&quyenxoa='+$('input[name$="quyenxoa"]').val();
			
		var urlaction = "modules/contract/AddHdct.php?id_row="+id_row+'&add='+add+'&gia_tri_moi='+gia_tri_moi+'&hct_id='+idsua+'&tien_chua_tt='+tien_chua_tt+'&'+getFormValues;
		$.getJSON(urlaction,null,function(data){
			if(add==0){
				$(data).appendTo("#divSelectedNames");
			}
			else{
				
				document.getElementById('row_sua_'+id_row).innerHTML = data;
			}
		});
		
		//xajax_addrowhdct(xajax.getFormValues('frmhd'),id_row,add,gia_tri_moi,idsua,tien_chua_tt);
		if(id_row==0){
			document.getElementById('bt_add_hdct').style.display='block';
			change_display('them_hdct');
			reset_them_moi_hdct();
		}
		
		document.getElementById('them_hdct').focus();
	}
}
function delete_hdct(id_row,id,tien_xoa,tong_tien,tienvat){
	so_hdct_ht=document.getElementById('count_hdct').value;
	var count_hdct = so_hdct_ht*1-1;
	jQuery("#row_sua_"+id_row).remove();
	document.getElementById('count_hdct').value=so_hdct_ht*1-1;
	var gia_tri_cu=document.getElementById('giatri_').value;
	var gia_tri_moi=gia_tri_cu*1-tien_xoa*1;
	var donhang = COREJS._getJsLang('donhang');
	var view = COREJS._getJsLang('view');
	document.getElementById('giatri_').value=gia_tri_moi;
	if(donhang!=0){
		tien_chua_tt=0;
		document.getElementById('giatri').innerHTML=formatNumber_DB(gia_tri_moi);
		//tien vat 
		/*tienvat_cu=document.getElementById('sum_tien_vat').value;
		tong_tien_vat=tienvat_cu*1-tienvat*1;
		document.getElementById('tien_vat_dd').innerHTML=formatNumber_DB(tong_tien_vat);
		document.getElementById('sum_tien_vat').value=tong_tien_vat;
		//tong tien
		tongtien_cu=document.getElementById('sum_tong_tien').value;
		tong_tien=tongtien_cu*1-tong_tien*1;
		document.getElementById('tong_tien_dd').innerHTML=formatNumber_DB(tong_tien);
		document.getElementById('sum_tong_tien').value=tong_tien;*/
	}
	else{
		var tien_da_tt=document.getElementById('tiendatt_').value;
		tien_chua_tt=gia_tri_moi*1-tien_da_tt*1;
		document.getElementById('tienphaitt_').value=tien_chua_tt;
		if(view == 1){
			document.getElementById('giatri').innerHTML=formatNumber_DB(gia_tri_moi);
			document.getElementById('tienphaitt').innerHTML=formatNumber_DB(tien_chua_tt);
		}
		else{
			document.getElementById('tienphaitt').value=formatNumber_DB(tien_chua_tt);
			document.getElementById('giatri').value=formatNumber_DB(gia_tri_moi);
		}
	}
	if(id!=''){
		table=document.getElementById('table').value;
		id_table=document.getElementById('id_table').value;
		//xoa hop dong chi tiet
		var urlaction ="modules/contract/deleteHDCT.php?id="+id+'&table='+table+'&gia_tri_moi='+gia_tri_moi+'&tien_chua_tt='+tien_chua_tt+'&id_table='+id_table;
			$.getJSON(urlaction,null,function(data){
			});
	}
	if(count_hdct == 0){
		document.getElementById('delete_row').style.display='block';
	}
}
/*
function show_spham(id,id_row){
	var urlaction = "modules/contract/showSanPham.php?iCls_Id="+id+'&id_row='+id_row;
	$.getJSON(urlaction,null,function(data){
		document.getElementById('arr_sp_'+id_row).innerHTML = data;
	});
}
*/

function show_spham(id)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp == null)
{
alert ("Browser does not support HTTP Request");
return;
}

var url = "modules/contract/showSanPham.php";
url = url + "?iCls_Id=" + id;

xmlhttp.onreadystatechange = stateChanged2;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function stateChanged2()
{
if (xmlhttp.readyState == 4)
{
document.getElementById("arr_sp_0").innerHTML = xmlhttp.responseText;

}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}


function check_gia_vat(sphid,soluong,id_row){
	var urlaction = "modules/contract/showGiaVat.php?Sph_Id="+sphid;
	$.getJSON(urlaction,null,function(data){
		var giaban = data.Gia_Ban;
		var vat = data.Vat;
		var dvt = data.Dvt_Id;
		var gia_codinh = data.Gia_Co_Dinh;
		jQuery("#giaban_"+id_row).val(formatNumber_DB(giaban));
		jQuery("#giaban_"+id_row+'_').val(giaban);
		jQuery("#gia_codinh_"+id_row).val(gia_codinh);
		if(gia_codinh == 1){
			jQuery("#giaban_"+id_row).attr('readonly',true);
		}
		else{
			jQuery("#giaban_"+id_row).removeAttr('readonly');
		}
		var tongcong = giaban*soluong;
		var tienvat=(tongcong*vat)/100;
		//tinh tong tien
		var thanhtien = tienvat*1 + tongcong*1;
		jQuery("#dvt_id_"+id_row).val(dvt) ;
		jQuery("#tien_tong_"+id_row).val(tongcong) ;
		jQuery("#tong_cdd_"+id_row).val(formatNumber_DB(tongcong)) ;
		jQuery("#vat_dd_"+id_row).val(formatNumber_DB(tienvat)) ;
		jQuery("#tien_vat_"+id_row).val(tienvat) ;
		jQuery("#vat_"+id_row).val(vat) ;
		jQuery("#thanhthien_"+id_row).val(thanhtien) ;
		jQuery("#thanhtien_dd_"+id_row).val(formatNumber_DB(thanhtien)) ;
	});
}
function counthdct(){
	so_hdct_ht=document.getElementById('count_hdct').value;
	var count_del = document.getElementById('count_del_hdct').value
	if(so_hdct_ht==""){
		count=1;
	}
	else{
		count=(so_hdct_ht*1)+1;
	}
	count_del = count_del*1 + 1;
	document.getElementById('display_count_hdct').innerHTML = count_del;
	document.getElementById('count_hdct').value = count;
	document.getElementById('count_del_hdct').value = count_del;
	
	if(count == 1){
		document.getElementById('delete_row').style.display='none';
	}
	
	document.getElementById('bt_add_hdct').style.display='none';
	document.getElementById('lsp_id_0').focus();
}
function tien_hdct(id_row){
	giaban=document.getElementById('giaban_'+id_row+'_').value;
	vat=document.getElementById('vat_'+id_row).value;
	soluong=document.getElementById('soluong_'+id_row).value;
	tongtien=(giaban*1)*(soluong*1);
	document.getElementById('tong_cdd_'+id_row).value = formatNumber_DB(tongtien);
	document.getElementById('tien_tong_'+id_row).value=tongtien;
	tienvat=(tongtien*vat)/100;
	document.getElementById('vat_dd_'+id_row).value=formatNumber_DB(tienvat);
	document.getElementById('tien_vat_'+id_row).value=tienvat;
	thanhtien=tienvat+tongtien;
	document.getElementById('thanhtien_dd_'+id_row).value=formatNumber_DB(thanhtien);
	document.getElementById('thanhthien_'+id_row).value=thanhtien;
}
function huy_them_hdct(){
	so_hdct_ht=document.getElementById('count_hdct').value;
	count=so_hdct_ht-1;
	count_del_hdct = document.getElementById('count_del_hdct').value;
	count_del = count_del_hdct -1;
	document.getElementById('display_count_hdct').innerHTML=count;
	document.getElementById('count_hdct').value = count;
	document.getElementById('count_del_hdct').value = count_del;
	document.getElementById('bt_add_hdct').style.display='block';
	change_display('them_hdct');
}
function reset_them_moi_hdct(){
	document.getElementById('soluong_0').value="";
	document.getElementById('giaban_0_').value="";
	document.getElementById('giaban_0').value="";
	document.getElementById('tien_tong_0').value="";
	document.getElementById('tong_cdd_0').value="";
	document.getElementById('tien_vat_0').value="";
	document.getElementById('vat_dd_0').value="";
	document.getElementById('thanhthien_0').value="";
	document.getElementById('thanhtien_dd_0').value="";
	document.getElementById('Sph_Id_0').value='';
	document.getElementById('dvt_id_0').value='';
	$('#vat_0').val('');
}
